package com.example.springpethospital.service;

import com.example.springpethospital.common.reponse.BaseResponse;
import com.example.springpethospital.common.reponse.EntityResponse;
import com.example.springpethospital.common.reponse.ListPagingResponse;
import com.example.springpethospital.common.reponse.ResponseCodeEnum;
import com.example.springpethospital.entity.examination.dto.ExaminationDTO;
import com.example.springpethospital.entity.examination.dto.ExaminationPaperDTO;
import com.example.springpethospital.entity.examination.param.ExaminationPaperQueryParam;
import com.example.springpethospital.entity.examination.param.ExaminationQueryParam;
import com.example.springpethospital.entity.examination.po.ExaminationPO;
import com.example.springpethospital.entity.examination.po.ExaminationPaperPO;
import com.example.springpethospital.entity.examination.po.ExaminationPaperPO.ExaminationPaperQuestion;
import com.example.springpethospital.entity.examination.po.ExaminationQuestionPO;
import com.example.springpethospital.entity.examination.param.ExaminationQuestionQueryParam;
import com.example.springpethospital.mapper.ExaminationMapper;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExaminationService {

    @Autowired
    private ExaminationMapper examinationMapper;

    public EntityResponse<Integer> questionCreate(ExaminationQuestionPO examinationQuestion) {
        examinationQuestion.check(false);
        int insert = examinationMapper.insertQuestion(examinationQuestion);
        if (insert == 0) {
            return new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "新建失败", null);
        }
        return new EntityResponse<>(ResponseCodeEnum.SUCCESS, "success", examinationQuestion.getId());
    }

    public BaseResponse questionUpdate(ExaminationQuestionPO examinationQuestion) {
        examinationQuestion.check(true);
        int update = examinationMapper.updateQuestion(examinationQuestion);
        if (update == 0) {
            return new BaseResponse(ResponseCodeEnum.FORBIDDEN, "更新失败");
        }
        return BaseResponse.success();
    }

    public ListPagingResponse<ExaminationQuestionPO> questionPage(ExaminationQuestionQueryParam queryParam) {
        int count = examinationMapper.queryQuestionCount(queryParam);
        if (count == 0) {
            return new ListPagingResponse<>(ResponseCodeEnum.SUCCESS, "success", Collections.emptyList(), queryParam.getStart(),
                queryParam.getLength(), 0);
        }
        List<ExaminationQuestionPO> examinationQuestions = examinationMapper.questionPage(queryParam);
        return new ListPagingResponse<>(ResponseCodeEnum.SUCCESS, "success", examinationQuestions, queryParam.getStart(),
            queryParam.getLength(), count);

    }

    public BaseResponse batchDeleteQuestion(List<Integer> questionIds) {
        if (CollectionUtils.isNotEmpty(questionIds)) {
            examinationMapper.batchDeleteQuestion(questionIds);
        }
        return BaseResponse.success();
    }

    public EntityResponse<Integer> paperCreate(ExaminationPaperPO param) {
        param.check(false);
        int insert = examinationMapper.insertPaper(param);
        if (insert == 0) {
            return new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "新建失败", null);
        }
        return new EntityResponse<>(ResponseCodeEnum.SUCCESS, "success", param.getId());
    }

    public BaseResponse paperUpdate(ExaminationPaperPO param) {
        param.check(true);
        int update = examinationMapper.updatePaper(param);
        if (update == 0) {
            return new BaseResponse(ResponseCodeEnum.FORBIDDEN, "更新失败");
        }
        return BaseResponse.success();
    }

    public ListPagingResponse<ExaminationPaperDTO> paperPage(ExaminationPaperQueryParam queryParam) {
        int count = examinationMapper.queryPaperCount(queryParam);
        if (count == 0) {
            return new ListPagingResponse<>(ResponseCodeEnum.SUCCESS, "success", Collections.emptyList(), queryParam.getStart(),
                queryParam.getLength(), 0);
        }
        List<ExaminationPaperPO> examinationPaperPOS = examinationMapper.paperPage(queryParam);
        Set<Integer> allQuestionIds = ListUtils.emptyIfNull(examinationPaperPOS).stream().flatMap(e -> e.getQuestions().stream().map(ExaminationPaperQuestion::getQuestionId))
            .collect(Collectors.toSet());
        List<ExaminationQuestionPO> questionPOS = examinationMapper
            .questionPage(ExaminationQuestionQueryParam.builder().ids(allQuestionIds).start(0).length(1000).build());
        List<ExaminationPaperDTO> convert = ExaminationPaperDTO.convert(examinationPaperPOS, questionPOS);
        return new ListPagingResponse<>(ResponseCodeEnum.SUCCESS, "success", convert, queryParam.getStart(),
            queryParam.getLength(), count);
    }

    public BaseResponse batchDeletePaper(List<Integer> paperIds) {
        if (CollectionUtils.isNotEmpty(paperIds)) {
            examinationMapper.batchDeletePaper(paperIds);
        }
        return BaseResponse.success();
    }

    public EntityResponse<Integer> examCreate(ExaminationPO param) {
        param.check(false);
        int insert = examinationMapper.insertExam(param);
        if (insert == 0) {
            return new EntityResponse<>(ResponseCodeEnum.FORBIDDEN, "新建失败", null);
        }
        return new EntityResponse<>(ResponseCodeEnum.SUCCESS, "success", param.getId());
    }

    public BaseResponse examUpdate(ExaminationPO param) {
        param.check(true);
        int update = examinationMapper.updateExam(param);
        if (update == 0) {
            return new BaseResponse(ResponseCodeEnum.FORBIDDEN, "更新失败");
        }
        return BaseResponse.success();
    }

    public ListPagingResponse<ExaminationDTO> examPage(ExaminationQueryParam queryParam) {
        int count = examinationMapper.queryExamCount(queryParam);
        if (count == 0) {
            return new ListPagingResponse<>(ResponseCodeEnum.SUCCESS, "success", Collections.emptyList(), queryParam.getStart(),
                queryParam.getLength(), 0);
        }
        List<ExaminationPO> examinations = examinationMapper.examPage(queryParam);
        Set<Integer> paperIds = ListUtils.emptyIfNull(examinations).stream().map(ExaminationPO::getPaperId).collect(Collectors.toSet());
        List<ExaminationPaperDTO> papers = this.paperPage(ExaminationPaperQueryParam.builder().ids(paperIds).start(0).length(1000).build()).getData();
        List<ExaminationDTO> convert = ExaminationDTO.convert(examinations, papers);
        return new ListPagingResponse<>(ResponseCodeEnum.SUCCESS, "success", convert, queryParam.getStart(),
            queryParam.getLength(), count);
    }

    public BaseResponse batchDeleteExam(List<Integer> examIds) {
        examinationMapper.batchDeleteExam(examIds);
        return BaseResponse.success();
    }
}
